package com.chengyu.core.service.shop.impl;

import com.chengyu.core.domain.form.UmsShopOwnLevelForm;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.*;
import com.chengyu.core.model.*;
import com.chengyu.core.service.shop.ShopLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @author LeGreen
 * @title 店铺接口
 * @date 2022/4/28
 */
@Service
public class ShopLevelServiceImpl implements ShopLevelService {

    @Autowired
    private UmsShopLevelPriceMapper shopLevelPriceMapper;
    @Autowired
    private UmsShopLevelMapper shopLevelMapper;
    @Autowired
    private UmsShopLevelPriceOwnMapper shopLevelPriceOwnMapper;
    @Autowired
    private UmsShopLevelOwnMapper shopLevelOwnMapper;
    @Autowired
    private UmsShopOwnLevelMapper shopOwnLevelMapper;
    @Autowired
    private UmsShopOwnLevelFormMapper umsShopOwnLevelFormMapper;
    @Autowired
    private OmsOrderShopLevelMapper omsOrderShopLevelMapper;
    @Autowired
    private FileInfoServiceMapper fileInfoMapper;
    @Autowired
    private UmsDeductionsDesignLogMapper deductionsDesignLogMapper;
    @Autowired
    private UmsShopMapper umsShopMapper;

    @Override
    public Map getShopLevelPriceById(Integer levelPriceId) throws ServiceException {
        UmsShopLevelPrice umsShopLevelPrice = shopLevelPriceMapper.selectByPrimaryKey(levelPriceId);
        //获取level信息
        UmsShopLevel umsShopLevel = shopLevelMapper.selectByPrimaryKey(umsShopLevelPrice.getLevelId());
        Map map = new HashMap();
        map.put("umsShopLevelPrice", umsShopLevelPrice);
        map.put("umsShopLevel", umsShopLevel);
        return map;
    }

    @Override
    public List<UmsShopLevel> listShopLevel(Boolean isShowInitLevel, Boolean isShowNoPrice, Integer isShow, String type) throws ServiceException {

        UmsShopLevelExample example = new UmsShopLevelExample();
        UmsShopLevelExample.Criteria criteria = example.createCriteria();
        if (!isShowInitLevel) {
            criteria.andIsInitLevelEqualTo(0);
        }
        if (isShow != null) {
            criteria.andIsShowEqualTo(isShow);
        }
        if (type != null) {
            String[] typeArray = type.split(",");
            List typeList = Arrays.asList(typeArray);
            criteria.andTypeIn(typeList);
        }
        List<UmsShopLevel> umsShopLevels = shopLevelMapper.selectByExample(example);
        List<UmsShopLevel> umsShopLevelsResult = new ArrayList<>();
        if (!isShowNoPrice) {
            //如果不显示没有价格的等级
            for (UmsShopLevel umsShopLevel : umsShopLevels) {
                UmsShopLevelPriceExample exampleCheck = new UmsShopLevelPriceExample();
                exampleCheck.createCriteria().andLevelIdEqualTo(umsShopLevel.getId()).andIsShowEqualTo(1);
                List<UmsShopLevelPrice> umsShopLevelPrices = shopLevelPriceMapper.selectByExample(exampleCheck);
                if (umsShopLevelPrices.size() > 0) {
                    umsShopLevelsResult.add(umsShopLevel);
                }
            }
        } else {
            umsShopLevelsResult = umsShopLevels;
        }


        return umsShopLevelsResult;
    }

    @Override
    public List<UmsShopLevelPrice> getShopLevelPriceByLevelId(Integer levelId, Integer isShow) throws ServiceException {
        UmsShopLevelPriceExample example = new UmsShopLevelPriceExample();
        UmsShopLevelPriceExample.Criteria criteria = example.createCriteria();
        criteria.andLevelIdEqualTo(levelId);
        if (isShow != null) {
            criteria.andIsShowEqualTo(isShow);
        }
        List<UmsShopLevelPrice> umsShopLevelPriceList = shopLevelPriceMapper.selectByExample(example);
        return umsShopLevelPriceList;
    }

    @Override
    public void updateShopLevel(UmsShopLevel umsShopLevel) throws ServiceException {
        if (umsShopLevel.getLevel() != null) {
            UmsShopLevelExample example = new UmsShopLevelExample();
            example.createCriteria().andLevelEqualTo(umsShopLevel.getLevel()).andIdNotEqualTo(umsShopLevel.getId());
            List<UmsShopLevel> umsShopLevels = shopLevelMapper.selectByExample(example);
            if (umsShopLevels.size() > 0) {
                throw new ServiceException("修改失败：等级已经存在");
            }
        }
        if (umsShopLevel.getName() != null) {
            UmsShopLevelExample example = new UmsShopLevelExample();
            example.createCriteria().andNameEqualTo(umsShopLevel.getName()).andIdNotEqualTo(umsShopLevel.getId());
            List<UmsShopLevel> umsShopLevels = shopLevelMapper.selectByExample(example);
            if (umsShopLevels.size() > 0) {
                throw new ServiceException("修改失败：等级名称已经存在");
            }
        }


        if (umsShopLevel.getIsInitLevel() != null && umsShopLevel.getIsInitLevel() != 0) {
            UmsShopLevelExample example = new UmsShopLevelExample();
            example.createCriteria().andIsInitLevelEqualTo(1).andIdNotEqualTo(umsShopLevel.getId());
            List<UmsShopLevel> umsShopLevels = shopLevelMapper.selectByExample(example);
            if (umsShopLevels.size() > 0) {
                //如果有初始等级了
                //先把所有的等级都设置为不初始化
                UmsShopLevel umsShopLevelUpdateInitLevel = new UmsShopLevel();
                UmsShopLevelExample umsShopLevelUpdateInitLevelExample = new UmsShopLevelExample();
                umsShopLevelUpdateInitLevel.setIsInitLevel(0);
                shopLevelMapper.updateByExampleSelective(umsShopLevelUpdateInitLevel, umsShopLevelUpdateInitLevelExample);
            } else if (umsShopLevel.getIsInitLevel() == 0) {
                throw new ServiceException("修改失败：至少要有一个等级作为初始等级");
            }
        }
        shopLevelMapper.updateByPrimaryKeySelective(umsShopLevel);
    }

    @Override
    public void updateShopLevelPrice(UmsShopLevelPrice umsShopLevelPrice) throws ServiceException {
        //查看除了自己是否已经有同样的时限了
        if (umsShopLevelPrice.getTimeType() != null) {
            UmsShopLevelPriceExample example = new UmsShopLevelPriceExample();
            example.createCriteria().andLevelIdEqualTo(umsShopLevelPrice.getLevelId()).andIdNotEqualTo(umsShopLevelPrice.getId()).andTimeTypeEqualTo(umsShopLevelPrice.getTimeType());
            List<UmsShopLevelPrice> umsShopLevelPrices = shopLevelPriceMapper.selectByExample(example);
            if (umsShopLevelPrices.size() > 0) {
                throw new ServiceException("修改失败：已经存在相同的时限了");
            }
        }

        shopLevelPriceMapper.updateByPrimaryKeySelective(umsShopLevelPrice);
    }

    @Override
    public void addShopLevel(UmsShopLevel umsShopLevel) throws ServiceException {
        if (umsShopLevel.getLevel() != null) {
            UmsShopLevelExample example = new UmsShopLevelExample();
            example.createCriteria().andLevelEqualTo(umsShopLevel.getLevel());
            List<UmsShopLevel> umsShopLevels = shopLevelMapper.selectByExample(example);
            if (umsShopLevels.size() > 0) {
                throw new ServiceException("修改失败：等级已经存在");
            }
        }
        if (umsShopLevel.getName() != null) {
            UmsShopLevelExample example = new UmsShopLevelExample();
            example.createCriteria().andNameEqualTo(umsShopLevel.getName());
            List<UmsShopLevel> umsShopLevels = shopLevelMapper.selectByExample(example);
            if (umsShopLevels.size() > 0) {
                throw new ServiceException("修改失败：等级名称已经存在");
            }
        }
        //如果新增的等级是初始等级先把所有的等级的初始显示修改为0
        if (umsShopLevel.getIsInitLevel() == 1) {
            UmsShopLevel umsShopLevelUpdateInitLevel = new UmsShopLevel();
            UmsShopLevelExample umsShopLevelUpdateInitLevelExample = new UmsShopLevelExample();
            umsShopLevelUpdateInitLevel.setIsInitLevel(0);
            shopLevelMapper.updateByExampleSelective(umsShopLevelUpdateInitLevel, umsShopLevelUpdateInitLevelExample);
        }

        umsShopLevel.setCode(UUID.randomUUID().toString());
        shopLevelMapper.insertSelective(umsShopLevel);
    }

    @Override
    public void addShopLevelPrice(UmsShopLevelPrice umsShopLevelPrice) throws ServiceException {
        //查看除了自己是否已经有同样的时限了
        if (umsShopLevelPrice.getTimeType() != null) {
            UmsShopLevelPriceExample example = new UmsShopLevelPriceExample();
            example.createCriteria().andLevelIdEqualTo(umsShopLevelPrice.getLevelId()).andTimeTypeEqualTo(umsShopLevelPrice.getTimeType());
            List<UmsShopLevelPrice> umsShopLevelPrices = shopLevelPriceMapper.selectByExample(example);
            if (umsShopLevelPrices.size() > 0) {
                throw new ServiceException("修改失败：已经存在相同的时限了");
            }
        }
        shopLevelPriceMapper.insertSelective(umsShopLevelPrice);
    }

    @Override
    public void deleteShopLevel(Integer id) {
        shopLevelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteShopLevelPrice(Integer id) {
        shopLevelPriceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UmsShopOwnLevelForm> getShopOwnLevelPriceById(Integer shopId) throws ServiceException {
        UmsShopOwnLevelExample example = new UmsShopOwnLevelExample();
        example.createCriteria().andShopIdEqualTo(shopId).andDeadlineGreaterThan(new Date());
        List<UmsShopOwnLevelForm> umsShopOwnLevels = umsShopOwnLevelFormMapper.selectByExample(example);
        for (UmsShopOwnLevelForm umsShopOwnLevel : umsShopOwnLevels) {
            UmsShopLevelPriceOwnExample umsShopLevelPriceOwnExample = new UmsShopLevelPriceOwnExample();
            umsShopLevelPriceOwnExample.createCriteria().andShopIdEqualTo(shopId).andIdEqualTo(umsShopOwnLevel.getLevelPriceOwnId());
            List<UmsShopLevelPriceOwn> umsShopLevelPrice = shopLevelPriceOwnMapper.selectByExample(umsShopLevelPriceOwnExample);
            UmsShopLevelPriceOwn umsShopLevelPriceOwn = umsShopLevelPrice.get(0);

            UmsShopLevelOwnExample umsShopLevelOwnExample = new UmsShopLevelOwnExample();
            umsShopLevelOwnExample.createCriteria().andShopIdEqualTo(shopId).andIdEqualTo(umsShopLevelPriceOwn.getLevelOwnId());
            List<UmsShopLevelOwn> umsShopLevel = shopLevelOwnMapper.selectByExample(umsShopLevelOwnExample);
            UmsShopLevelOwn umsShopLevelOwn = umsShopLevel.get(0);

            umsShopOwnLevel.setUmsShopLevelOwn(umsShopLevelOwn);
            umsShopOwnLevel.setUmsShopLevelPriceOwn(umsShopLevelPriceOwn);

            if (umsShopLevelOwn.getType() == 1 || umsShopLevelOwn.getType() == 2) {
                //查看此商家所有的文件量
                FileInfoExample fileInfoExample = new FileInfoExample();
                FileInfoExample.Criteria criteria = fileInfoExample.createCriteria();
                criteria.andSourceEqualTo(1);
                criteria.andSourceIdEqualTo(shopId);
                double totalUseSize = fileInfoMapper.sumSize(fileInfoExample);
                double totalUseSizeGb = totalUseSize / 1073741824.0;
                DecimalFormat df = new DecimalFormat("#.##");
                String format = df.format(Double.parseDouble(umsShopLevelOwn.getStorageQuantity().toString()) - totalUseSizeGb);
                umsShopOwnLevel.setResidueStorageQuantity(Double.parseDouble(format));
            }

        }


        Collections.sort(umsShopOwnLevels, new Comparator<UmsShopOwnLevelForm>() {
            @Override
            public int compare(UmsShopOwnLevelForm o1, UmsShopOwnLevelForm o2) {
                return o2.getUmsShopLevelOwn().getLevel() - o1.getUmsShopLevelOwn().getLevel(); // 从大到小排序
            }
        });
        return umsShopOwnLevels;
    }

    @Override
    public void updateOwnLevel(UmsShopOwnLevel umsShopOwnLevel) throws ServiceException {
        shopOwnLevelMapper.updateByPrimaryKeySelective(umsShopOwnLevel);
    }

    @Override
    public void addShopLevelOrder(OmsOrderShopLevel omsOrderShopLevel) throws ServiceException {
        omsOrderShopLevelMapper.insert(omsOrderShopLevel);
    }

    @Override
    public OmsOrderShopLevel getByOrderNum(String orderNo) throws ServiceException {
        OmsOrderShopLevelExample example = new OmsOrderShopLevelExample();
        OmsOrderShopLevelExample.Criteria criteria = example.createCriteria();
        criteria.andOrderNoEqualTo(orderNo);
        List<OmsOrderShopLevel> omsOrderShopLevels = omsOrderShopLevelMapper.selectByExample(example);
        if (omsOrderShopLevels.size() == 0) {
            return null;
        }


        return omsOrderShopLevels.get(0);
    }

    @Override
    public void addShopOwnLevel(UmsShopOwnLevel umsShopOwnLevel) throws ServiceException {
        shopOwnLevelMapper.insert(umsShopOwnLevel);
    }

    @Override
    public void checkShopLevel(Integer shopId) throws ServiceException {
        UmsShopOwnLevelExample shopOwnLevelExample = new UmsShopOwnLevelExample();
        UmsShopOwnLevelExample.Criteria criteria = shopOwnLevelExample.createCriteria();
        criteria.andShopIdEqualTo(shopId);
        criteria.andTypeEqualTo(1);
        criteria.andDeadlineGreaterThan(new Date());
        List<UmsShopOwnLevel> umsShopOwnLevels = shopOwnLevelMapper.selectByExample(shopOwnLevelExample);
        if (umsShopOwnLevels.size() == 0) {
            throw new ServiceException("您还不是会员，无法操作，请您购买会员");
        }
    }

    @Override
    public List<UmsShopOwnLevelForm> getShopOwnLevelList() throws ServiceException {
        UmsShopOwnLevelExample example = new UmsShopOwnLevelExample();
        example.createCriteria().andUiLayoutNumGreaterThan(0);
        example.or().andGoodsMainImgDesignNumGreaterThan(0);
        List<UmsShopOwnLevelForm> umsShopOwnLevelForms = umsShopOwnLevelFormMapper.selectByExample(example);
        return umsShopOwnLevelForms;
    }

    @Override
    public void deductions(Integer type, Integer id, String editName) throws ServiceException {
        UmsShopOwnLevel umsShopOwnLevel = shopOwnLevelMapper.selectByPrimaryKey(id);
        UmsShopOwnLevel shopOwnLevelUpdate = new UmsShopOwnLevel();
        shopOwnLevelUpdate.setId(id);
        if (type == 1) {
            //如果是扣减ui数量
            if (umsShopOwnLevel.getUiLayoutNum() > 0) {
                shopOwnLevelUpdate.setUiLayoutNum(umsShopOwnLevel.getUiLayoutNum() - 1);
            }

        } else if (type == 2) {
            //如果是扣减主图
            if (umsShopOwnLevel.getGoodsMainImgDesignNum() > 0) {
                shopOwnLevelUpdate.setGoodsMainImgDesignNum(umsShopOwnLevel.getGoodsMainImgDesignNum() - 1);
            }
        }
        shopOwnLevelMapper.updateByPrimaryKeySelective(shopOwnLevelUpdate);
        //添加扣减记录
        UmsDeductionsDesignLog umsDeductionsDesignLog = new UmsDeductionsDesignLog();
        umsDeductionsDesignLog.setNum(1);
        umsDeductionsDesignLog.setShopId(umsShopOwnLevel.getShopId());
        UmsShop shop = umsShopMapper.selectByPrimaryKey(umsShopOwnLevel.getShopId());
        umsDeductionsDesignLog.setShopName(shop.getName());
        umsDeductionsDesignLog.setShopOwnLevelId(shopOwnLevelUpdate.getId());
        umsDeductionsDesignLog.setType(type);
        umsDeductionsDesignLog.setAddTime(new Date());
        umsDeductionsDesignLog.setAddBy(editName);
        deductionsDesignLogMapper.insert(umsDeductionsDesignLog);

    }

    @Override
    public List<UmsDeductionsDesignLog> deductionsList() throws ServiceException {
        UmsDeductionsDesignLogExample example = new UmsDeductionsDesignLogExample();
        List<UmsDeductionsDesignLog> umsDeductionsDesignLogs = deductionsDesignLogMapper.selectByExample(example);
        return umsDeductionsDesignLogs;
    }
}